﻿using bin.fontconv;
using System.Diagnostics;
using ttbit.form;

namespace bin.pixelfontconv
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("这个工具用来导出区位字库");
            if (args.Length == 0)
            {
                Console.WriteLine("自动加载区位转换表 Utf16conv.bin");
                Console.WriteLine("根据字体名字中的数字决定字号，根据字体名字中是否含有ASC作为英文字库，根据名字中是否含有hzk作为汉字库");
                Console.WriteLine("用法bin.PixelFontConv ASCII12.bin HZK12.bin");
                return;
            }
            using var convstream = System.IO.File.OpenRead("UTF16CONV.bin");
            fontconv.FontTool.LoadGB2Utf16(convstream);
            List<fontconv.FontTool.Font> fonts = new List<FontTool.Font>();
            foreach (var font in args)
            {
                bool isAscii = false;
                bool isHZK = false;
                if (font.ToLower().IndexOf("asc") >= 0)
                {
                    isAscii = true;
                    string numstr = "";
                    foreach (var c in font)
                    {
                        if (char.IsNumber(c))
                            numstr += c;
                    }
                    if (int.TryParse(numstr, out int v))
                    {
                        using var fontstream = System.IO.File.OpenRead(font);
                        var pfont = FontTool.LoadAscII((byte)v, System.IO.Path.GetFileNameWithoutExtension(font), fontstream);
                        fonts.Add(pfont);
                        Console.WriteLine("Find a Ascii font:" + pfont.name + " size:" + pfont.height);
                        continue;
                    }
                }
                if (font.ToLower().IndexOf("hzk") >= 0)
                {
                    isHZK = true;
                    string numstr = "";
                    foreach (var c in font)
                    {
                        if (char.IsNumber(c))
                            numstr += c;
                    }
                    if (int.TryParse(numstr, out int v))
                    {
                        using var fontstream = System.IO.File.OpenRead(font);
                        var pfont = FontTool.LoadQuWei(v, System.IO.Path.GetFileNameWithoutExtension(font), fontstream);
                        fonts.Add(pfont);
                        Console.WriteLine("Find a hzk font:" + pfont.name + " size:" + pfont.height);
                        continue;
                    }
                }
            }
            Console.WriteLine("collect font now gogogo.");

            foreach (var font in fonts)
            {
                var fontData = new FontData();
                fontData.header = new FontData.Header();
                fontData.header.fontSize = (byte)font.height;
                fontData.header.charCount = (uint)font.graydata.Count;
                fontData.header.fontBorder = 0;
                fontData.fontItems = new Dictionary<uint, FontData.Item>();
                foreach (var dat in font.graydata)
                {
                    var item = new FontData.Item();
                    item.charCode = dat.Key;
                    item.width = (byte)font.width;
                    item.height = (byte)font.height;
                    item.emptyX = 0;
                    item.emptyY = 0;
                    item.advanceX = (byte)font.width;
                    if (font.width == font.height)
                        item.graydata = dat.Value;
                    else
                    {//填充
                        item.graydata = new byte[font.height * font.height];
                        for (var y = 0; y < font.height; y++)
                        {
                            for (var x = 0; x < font.width; x++)
                            {
                                item.graydata[y * font.height + x] = dat.Value[y * font.width + x];
                            }
                        }
                    }
                    fontData.fontItems[dat.Key] = item;
                }
                var outfilename = font.name + ".ttfont";
                System.IO.File.Delete(outfilename);
                using (var f = System.IO.File.OpenWrite(outfilename))
                {
                    using var ws = new System.IO.BinaryWriter(f);
                    fontData.Write(ws);
                }
            }

        }
    }
}